//
//  BCWalleteBalanceView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/15.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCWalleteBalanceView.h"

#import "BCUserWalletResponse.h"

//顶部底部的间距
static CGFloat BCWalleteBalanceViewTopBottomPadding =  22.0f;
//小号字体视图的高度
static CGFloat BCWalleteBalanceViewSmallFontHeight =  34.0f;
//大号字体视图的高度
static CGFloat BCWalleteBalanceViewLargeFontHeight =  38.0f;

@interface BCWalleteBalanceView ()

@property (nonatomic, strong) QMUIButton *balanceButton;
/**
 *balanceButtonArrow
 */
@property (nonatomic, strong) UIImageView *balanceButtonArrow;

@property (nonatomic, strong) UILabel *balanceLabel;

@property (nonatomic, strong) UILabel *safeLabel;

@end

@implementation BCWalleteBalanceView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
        //
//        [self initContents];
    }
    return self;
}
- (void)setupSubviews {
    self.balanceButton = [QMUIButton new];
    self.balanceButton.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    [self addSubview:self.balanceButton];
    [self.balanceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(BCWalleteBalanceViewTopBottomPadding);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(BCWalleteBalanceViewSmallFontHeight);
        make.width.mas_greaterThanOrEqualTo(80.0);
    }];
    [self.balanceButton addTarget:self action:@selector(balanceButtonDidClickedAction) forControlEvents:UIControlEventTouchUpInside];
    self.balanceButtonArrow = [UIImageView new];
    self.balanceButtonArrow.image = [LOAD_IMAGE(@"wallet_balance_arrow") imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
    [self.balanceButton addSubview:self.balanceButtonArrow];
    [self.balanceButtonArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.balanceButton);
        make.left.mas_equalTo(self.balanceButton.titleLabel.mas_right).offset(5.0);
    }];
//    self.balanceButton.userInteractionEnabled = false;
    
    //
    self.balanceLabel = [UILabel new];
    self.balanceLabel.font = [UIFont systemFontOfSize:24 weight:UIFontWeightRegular];
    self.balanceLabel.textAlignment = NSTextAlignmentCenter;
    self.balanceLabel.adjustsFontSizeToFitWidth = true;
    [self addSubview:self.balanceLabel];
    [self.balanceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.balanceButton.mas_bottom);
        make.centerX.mas_equalTo(self);
        make.height.mas_equalTo(BCWalleteBalanceViewLargeFontHeight);
        make.width.mas_equalTo(self).offset(-20.0);
    }];
    //
    self.safeLabel = [UILabel new];
    self.safeLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
    [self addSubview:self.safeLabel];
    [self.safeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.balanceLabel.mas_bottom);
        make.centerX.mas_equalTo(self);
    }];
}
- (void)setBackgroundColor:(UIColor *)backgroundColor {
    [super setBackgroundColor:backgroundColor];
    //
    UIColor *textColor = [UIColor lightTextColor];
    if (![backgroundColor qmui_colorIsDark]) {
        textColor = [UIColor darkTextColor];
    }
    self.balanceLabel.textColor = textColor;
    self.safeLabel.textColor = textColor;
    [self.balanceButton setTitleColor:textColor forState:UIControlStateNormal];
    //
    self.balanceButtonArrow.tintColor = textColor;
}
//初始化设置
- (void)initContents {
    //
    self.balanceLabel.text = @"--";
    //
    self.safeLabel.text = BCLocalizedKey(@"wallet_safe_security");
}

- (void)setBalance:(NSString *)balance {
    _balance = balance;
    //
    self.balanceLabel.text = balance?:@"--";
}


- (void)setWalletData:(BCUserWalletData *)walletData {
    _walletData = walletData;
    //
    NSString *market = [walletData.flow.market stringByReplacingOccurrencesOfString:@"{}" withString:walletData.monetaryUnit];
    [self.balanceButton setTitle:market forState:UIControlStateNormal];
    //
    self.balance = [NSString stringWithFormat:@"%@",[walletData.currency objectForKey:walletData.monetaryUnit]];
    //
    self.safeLabel.text = walletData.flow.safety;
}



#pragma mark - balanceButtonDidClickedAction
- (void)balanceButtonDidClickedAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(walletBalnaceView:didClickedBalanceButtonWithArrowConvertFrame:)]) {
        //转换arrow的frame为self的frame 即为arrowConvertFrame
        CGRect arrowConvertFrame = [self.balanceButton convertRect:self.balanceButtonArrow.frame toView:self];
        [self.delegate walletBalnaceView:self didClickedBalanceButtonWithArrowConvertFrame:arrowConvertFrame];
    }
}




+ (CGFloat)defaultHeight {
    CGFloat height = 0;
    height += BCWalleteBalanceViewTopBottomPadding;
    height += BCWalleteBalanceViewSmallFontHeight;
    height += BCWalleteBalanceViewLargeFontHeight;
    height += BCWalleteBalanceViewSmallFontHeight;
    height += 17.0;
    return height;
}

@end
